<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">赛课后台管理系统</div>
      <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content" @keyup.enter="login()">
        <el-form-item prop="username">
          <el-input v-model="param.username" placeholder="请输入账号">
            <i class="el-icon-user el-input__icon" size="22"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" placeholder="请输入密码" v-model="param.password">
            <i class="el-icon-lock el-input__icon"></i>
          </el-input>
        </el-form-item>
        <div class="login-btn">
            <el-button :loading="loading" @click="login()" >登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
import axios from "axios";

export default {
  data() {
    return {
      loading: false,
      param: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    login() {
      const data = {
        username: this.param.username,
        password: this.param.password,
      };

      axios.post('https://api.saike.qqwj.live/admin_login', data)
          .then(response => {
            if (response.status === 200) {
              // 登录成功，将用户信息保存到localStorage
              localStorage.setItem('token', response.data.token);
              localStorage.setItem('userInfo', JSON.stringify(response.data));


              // 跳转到其他页面或执行其他操作
              this.$router.push("/admin/race_list"); // 跳转到仪表板页面
            } else {
              // 处理登录失败逻辑
              this.$message.error("登录失败，请检查用户名和密码");
            }
          })
          .catch(error => {
            console.error("登录请求失败:", error);
          });
    },
  },
};
</script>
<style scoped>
.login-wrap {
  position: relative;
  width: 100vw;
  height: 100vh;
  background-image: url(../assets/login.jpg);
  background-size: cover;
}

.ms-title {
  width: 100%;
  line-height: 35px;
  margin-top: 35px;
  margin-bottom: -15px;
  color: #333333;
  text-align: center;
  font-size: 28px;
  font-weight: bolder;
  letter-spacing: 4px;
}

.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 345px;
  margin: -220px 0 0 -172px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 1);
  overflow: hidden;
}

.ms-content {
  padding: 50px 30px;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
  font-size: 14px;
  margin-top: 10px;
  margin-bottom: 0px;
  letter-spacing: 10px;
}

.login-tips {
  font-size: 12px;
  line-height: 30px;
  color: #FFFFFF;
}

.login-wrap .el-form-item--small.el-form-item {
  margin-bottom: 28px;
}

/* >>> .el-input__inner { */
:deep( .el-input__inner) {
  height: 40px;
  font-size: 15px;
}

.el-input--small .el-input__icon {
  font-size: 18px;
  padding-right: 10px;
  line-height: 40px;
}
</style>
<style>
.el-icon-loading {
  width: 14px !important;
}
</style>
